package com.it.water.controller.HttpController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.water.common.Result;
import com.it.water.entity.MonitoringEvents;
import com.it.water.service.IMonitoringEventsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 事件信息控制器
 *
 * @author <a href="mailto:2038322151@qq.com">Miraitowa_zcx</a>
 * @author yjh
 * @author HelloWorld505
 * @version 1.0
 * @since 2024-03-19
 */
@Slf4j
@RestController
@RequestMapping("/event")
@CrossOrigin
public class MonitoringEventsController {

    /**
     * 事件信息服务
     */
    private final IMonitoringEventsService monitoringEventsService;

    /**
     * 注入事件信息服务
     *
     * @param monitoringEventsService 事件信息服务
     */
    @Autowired
    public MonitoringEventsController(IMonitoringEventsService monitoringEventsService) {
        this.monitoringEventsService = monitoringEventsService;
    }

    /**
     * 事件信息分页查询
     *
     * @param page     分页页码
     * @param pageSize 分页大小
     * @param name     查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<MonitoringEvents>> page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false, defaultValue = "") String name) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);

        // 构造分页构造器
        Page<MonitoringEvents> pageInfo = new Page<>(page, pageSize);
        // 设置页数
        pageInfo.setCurrent(page);
        // 设置每页显示的记录数
        pageInfo.setSize(pageSize);
        // 构造条件构造器
        QueryWrapper<MonitoringEvents> queryWrapper = new QueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), "description", name);
        // 添加排序条件
        queryWrapper.orderByDesc("id");
        // 设置总记录数
        pageInfo.setTotal(monitoringEventsService.count(queryWrapper));
        // 执行查询
        monitoringEventsService.page(pageInfo, queryWrapper);

        return Result.success(pageInfo);
    }

    /**
     * 新增事件信息
     *
     * @param monitoringEvents 事件信息
     * @return 新增结果
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody MonitoringEvents monitoringEvents) {
        log.info("新增事件信息：{}", monitoringEvents.toString());
        monitoringEventsService.save(monitoringEvents);
        return Result.success("新增事件成功");
    }

    /**
     * 修改事件信息
     *
     * @param monitoringEvents 事件信息
     * @return 修改结果
     */
    @PutMapping("/update/{id}")
    public Result<String> update(@RequestBody MonitoringEvents monitoringEvents) {
        log.info(monitoringEvents.toString());
        monitoringEventsService.updateById(monitoringEvents);
        return Result.success("设备事件信息修改成功");
    }

    /**
     * 根据ID查询事件信息
     *
     * @param id 事件ID
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<MonitoringEvents> getById(@PathVariable Integer id) {
        log.info("根据id查询事件信息...");
        MonitoringEvents monitoringEvents = monitoringEventsService.getById(id);
        if (monitoringEvents != null) {
            return Result.success(monitoringEvents);
        }
        return Result.error("没有查询到对应设备信息");
    }

    /**
     * 删除事件信息
     *
     * @param id 事件ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        log.info("根据id删除事件信息...");
        monitoringEventsService.removeById(id);
        return Result.success("删除事件成功");
    }
}
